$().ready(function() {
	validateRule();

	$("#types").val( sessionStorage.getItem('types') )

	$.ajax({
		cache : true,
		type : "get",
		url : "/jzApi/examinations/selExaminationCom",
		data:{
			type: sessionStorage.getItem('types')
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#cbatchId').selectPage({
					showField: 'name',
					keyField: 'id',
					data: data.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
					eSelect : function(data){
						$('#cbatchId').val(data.id);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

	$.ajax({
		type : "post",
		url : "/jzweb/trackDrawRegular/dictDOList",
		headers: { 'Content-Type': 'application/json;charset=UTF-8' },
		data: JSON.stringify({type: 'trackDrawConfigSubset'}),
		async : false,
		success: function(response) {
			if (response.code == 0) {
				$('#trackDraw').selectPage({
					showField: 'name',
					keyField: 'name',
					data: response.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
				});

			} else {
				parent.layer.alert(data.msg)
			}
		},
		error: function(error) {
			layer.msg(error);
		}
	});

});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

// function gettrack() {
// 	return  new Promise((resolve, reject) => {
// 		try{
// 			$.ajax({
// 				type : "post",
// 				url : prefix +"/dictDOList",
// 				headers: { 'Content-Type': 'application/json;charset=UTF-8' },
// 				data: JSON.stringify({type: 'trackDrawConfigSubset'}),
// 				async : false,
// 				success: function(response) {
// 					if(response.code === '0') {
// 						resolve(response.data)
// 						return
// 					}else {
// 						reject(response)
// 						layer.msg(response.msg);
// 					}
// 				},
// 				error: function(error) {
// 					reject(error)
// 					layer.msg(error);
// 				}
// 			});
// 		}catch (e) {
// 			layer.msg(e);
// 		}
// 	})
// }

function save() {

	$("#types").val(sessionStorage.getItem('types'));

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/stuSignUpCom/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			idNumber : {
				required : true
			},
			ticketNumber : {
				required : true
			},
			cbatchId : {
				required : true
			},
			unitName : {
				required : true
			},
			trackDraw : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			},
			idNumber : {
				required : icon + "请输入身份证号码"
			},
			ticketNumber : {
				required : icon + "请输入考号/手机号码"
			},
			cbatchId : {
				required : icon + "请选择考试批次"
			},
			unitName : {
				required : icon + "请输入单位名称/学校名称"
			},
			trackDraw : {
				required : icon + "请选择场次信息"
			}
		}
	})
}